home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / GIF2IRIS.C < prev    next >
C/C++ Source or Header  |  1991-05-12  |  9KB  |  284 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber            UNIX Ver 0.1,    Jul. 1989    *
  5. ******************************************************************************
  6. * Program to display GIF file under X11 window system.                 *
  7. * Options:                                     *
  8. * -q : quite printing mode.                             *
  9. * -f : force the process to be in foreground.                     *
  10. * -p PosX PosY : defines the position where to put the image.             *
  11. * -h : on line help.                                 *
  12. ******************************************************************************
  13. * History:                                     *
  14. * 13 mar 90 - Version 1.0 by Gershon Elber.                     *
  15. *****************************************************************************/
  16.  
  17. #ifdef __MSDOS__
  18. #include <graphics.h>
  19. #include <stdlib.h>
  20. #include <alloc.h>
  21. #include <io.h>
  22. #include <dos.h>
  23. #include <bios.h>
  24. #endif /* __MSDOS__ */
  25.  
  26. #include "gl.h"
  27. #include "device.h"
  28.  
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #include <string.h>
  32. #include <fcntl.h>
  33. #include "gif_lib.h"
  34. #include "getarg.h"
  35.  
  36. #define PROGRAM_NAME    "Gif2Iris"
  37.  
  38. #define ABS(x)        ((x) > 0 ? (x) : (-(x)))
  39.  
  40. #ifdef __MSDOS__
  41. extern unsigned int
  42.     _stklen = 16384;                 /* Increase default stack size. */
  43. #endif /* __MSDOS__ */
  44. #ifdef SYSV
  45. static char *VersionStr =
  46.         "Gif library module,\t\tGershon Elber\n\
  47.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  48. static char
  49.     *CtrlStr = "Gif2Iris q%- f%- p%-PosX|PosY!d!d h%- GifFile!*s";
  50. #else
  51. static char
  52.     *VersionStr =
  53.     PROGRAM_NAME
  54.     GIF_LIB_VERSION
  55.     "    Gershon Elber,    "
  56.     __DATE__ ",   " __TIME__ "\n"
  57.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  58. static char
  59.     *CtrlStr =
  60.     PROGRAM_NAME
  61.     " q%- f%- p%-PosX|PosY!d!d h%- GifFile!*s";
  62. #endif /* SYSV */
  63.  
  64. /* Make some variables global, so we could access them faster: */
  65. static int
  66.     PosFlag = FALSE,
  67.     HelpFlag = FALSE,
  68.     ForeGroundFlag = FALSE,
  69.     ColorMapSize = 0,
  70.     BackGround = 0,
  71.     IrisPosX = 0,
  72.     IrisPosY = 0,
  73.     InterlacedOffset[] = { 0, 4, 2, 1 }, /* The way Interlaced image should. */
  74.     InterlacedJumps[] = { 8, 8, 4, 2 };    /* be read - offsets and jumps... */
  75. static GifColorType
  76.     *ColorMap;
  77.  
  78. static void Screen2Iris(GifRowType *ScreenBuffer,
  79.             int ScreenWidth, int ScreenHeight);
  80.  
  81. /******************************************************************************
  82. * Interpret the command line and scan the given GIF file.              *
  83. ******************************************************************************/
  84. void main(int argc, char **argv)
  85. {
  86.     int    i, j, Error, NumFiles, ImageNum = 0, Size, Row, Col, Width, Height,
  87.         ExtCode, Count;
  88.     GifRecordType RecordType;
  89.     GifByteType *Extension;
  90.     char **FileName = NULL;
  91.     GifRowType *ScreenBuffer;
  92.     GifFileType *GifFile;
  93.  
  94.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  95.         &GifQuitePrint, &ForeGroundFlag,
  96.         &PosFlag, &IrisPosX, &IrisPosY,
  97.         &HelpFlag, &NumFiles, &FileName)) != FALSE ||
  98.         (NumFiles > 1 && !HelpFlag)) {
  99.     if (Error)
  100.         GAPrintErrMsg(Error);
  101.     else if (NumFiles > 1)
  102.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  103.     GAPrintHowTo(CtrlStr);
  104.     exit(1);
  105.     }
  106.  
  107.     if (HelpFlag) {
  108.     fprintf(stderr, VersionStr);
  109.     GAPrintHowTo(CtrlStr);
  110.     exit(0);
  111.     }
  112.  
  113.     if (NumFiles == 1) {
  114.     if ((GifFile = DGifOpenFileName(*FileName)) == NULL) {
  115.         PrintGifError();
  116.         exit(-1);
  117.     }
  118.     }
  119.     else {
  120.     /* Use the stdin instead: */
  121.  
  122. #ifdef __MSDOS__
  123.     setmode(0, O_BINARY);
  124. #endif /* __MSDOS__ */
  125.     if ((GifFile = DGifOpenFileHandle(0)) == NULL) {
  126.         PrintGifError();
  127.         exit(-1);
  128.     }
  129.     }
  130.  
  131.     /* Allocate the screen as vector of column of rows. We cannt allocate    */
  132.     /* the all screen at once, as this broken minded CPU can allocate up to  */
  133.     /* 64k at a time and our image can be bigger than that:             */
  134.     /* Note this screen is device independent - its the screen as defined by */
  135.     /* the GIF file parameters itself.                         */
  136.     if ((ScreenBuffer = (GifRowType *)
  137.     malloc(GifFile -> SHeight * sizeof(GifRowType *))) == NULL)
  138.         GIF_EXIT("Failed to allocate memory required, aborted.");
  139.  
  140.     Size = GifFile -> SWidth * sizeof(GifPixelType);/* Size in bytes one row.*/
  141.     if ((ScreenBuffer[0] = (GifRowType) malloc(Size)) == NULL) /* First row. */
  142.     GIF_EXIT("Failed to allocate memory required, aborted.");
  143.  
  144.     for (i = 0; i < GifFile -> SWidth; i++)  /* Set its color to BackGround. */
  145.     ScreenBuffer[0][i] = GifFile -> SBackGroundColor;
  146.     for (i = 1; i < GifFile -> SHeight; i++) {
  147.     /* Allocate the other rows, and set their color to background too: */
  148.     if ((ScreenBuffer[i] = (GifRowType) malloc(Size)) == NULL)
  149.         GIF_EXIT("Failed to allocate memory required, aborted.");
  150.  
  151.     memcpy(ScreenBuffer[i], ScreenBuffer[0], Size);
  152.     }
  153.  
  154.     /* Scan the content of the GIF file and load the image(s) in: */
  155.     do {
  156.     if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) {
  157.         PrintGifError();
  158.         exit(-1);
  159.     }
  160.     switch (RecordType) {
  161.         case IMAGE_DESC_RECORD_TYPE:
  162.         if (DGifGetImageDesc(GifFile) == GIF_ERROR) {
  163.             PrintGifError();
  164.             exit(-1);
  165.         }
  166.         Row = GifFile -> ITop; /* Image Position relative to Screen. */
  167.         Col = GifFile -> ILeft;
  168.         Width = GifFile -> IWidth;
  169.         Height = GifFile -> IHeight;
  170.         GifQprintf("\n%s: Image %d at (%d, %d) [%dx%d]:     ",
  171.             PROGRAM_NAME, ++ImageNum, Col, Row, Width, Height);
  172.         if (GifFile -> ILeft + GifFile -> IWidth > GifFile -> SWidth ||
  173.            GifFile -> ITop + GifFile -> IHeight > GifFile -> SHeight) {
  174.             fprintf(stderr, "Image %d is not confined to screen dimension, aborted.\n");
  175.             exit(-2);
  176.         }
  177.         if (GifFile -> IInterlace) {
  178.             /* Need to perform 4 passes on the images: */
  179.             for (Count = i = 0; i < 4; i++)
  180.             for (j = Row + InterlacedOffset[i]; j < Row + Height;
  181.                          j += InterlacedJumps[i]) {
  182.                 GifQprintf("\b\b\b\b%-4d", Count++);
  183.                 if (DGifGetLine(GifFile, &ScreenBuffer[j][Col],
  184.                 Width) == GIF_ERROR) {
  185.                 PrintGifError();
  186.                 exit(-1);
  187.                 }
  188.             }
  189.         }
  190.         else {
  191.             for (i = 0; i < Height; i++) {
  192.             GifQprintf("\b\b\b\b%-4d", i);
  193.             if (DGifGetLine(GifFile, &ScreenBuffer[Row++][Col],
  194.                 Width) == GIF_ERROR) {
  195.                 PrintGifError();
  196.                 exit(-1);
  197.             }
  198.             }
  199.         }
  200.         break;
  201.         case EXTENSION_RECORD_TYPE:
  202.         /* Skip any extension blocks in file: */
  203.         if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR) {
  204.             PrintGifError();
  205.             exit(-1);
  206.         }
  207.         while (Extension != NULL) {
  208.             if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) {
  209.             PrintGifError();
  210.             exit(-1);
  211.             }
  212.         }
  213.         break;
  214.         case TERMINATE_RECORD_TYPE:
  215.         break;
  216.         default:            /* Should be traps by DGifGetRecordType. */
  217.         break;
  218.     }
  219.     }
  220.     while (RecordType != TERMINATE_RECORD_TYPE);
  221.  
  222.     /* Lets display it - set the global variables required and do it: */
  223.     BackGround = GifFile -> SBackGroundColor;
  224.     ColorMap = (GifFile -> IColorMap ? GifFile -> IColorMap :
  225.                        GifFile -> SColorMap);
  226.     ColorMapSize = 1 << (GifFile -> IColorMap ? GifFile -> IBitsPerPixel :
  227.                                 GifFile -> SBitsPerPixel);
  228.     GifQprintf("\n");
  229.     Screen2Iris(ScreenBuffer, GifFile -> SWidth, GifFile -> SHeight);
  230.  
  231.     if (DGifCloseFile(GifFile) == GIF_ERROR) {
  232.     PrintGifError();
  233.     exit(-1);
  234.     }
  235. }
  236.  
  237. /******************************************************************************
  238. * The real display routine.                              *
  239. ******************************************************************************/
  240. static void Screen2Iris(GifRowType *ScreenBuffer,
  241.             int ScreenWidth, int ScreenHeight)
  242. {
  243.     short Val;
  244.     int i, j;
  245.     unsigned long *IrisScreenBuffer, *PBuffer;
  246.  
  247.     if (ScreenWidth > XMAXSCREEN + 1 ||
  248.     ScreenHeight > YMAXSCREEN + 1)
  249.     GIF_EXIT("Input image is too big.");
  250.  
  251.     if (PosFlag)
  252.     prefposition(IrisPosX, IrisPosX + ScreenWidth - 1,
  253.              IrisPosY, IrisPosY + ScreenHeight - 1);
  254.     else
  255.     prefsize(ScreenWidth, ScreenHeight);
  256.     if (ForeGroundFlag)
  257.     foreground();
  258.  
  259.     winopen(PROGRAM_NAME);
  260.     RGBmode();
  261.     gconfig();
  262.     if ((IrisScreenBuffer = (unsigned long *)
  263.      malloc(ScreenWidth * ScreenHeight * sizeof(unsigned long))) == NULL)
  264.     GIF_EXIT("Failed to allocate memory required, aborted.");
  265.  
  266.     PBuffer = IrisScreenBuffer;
  267.     for (i = ScreenHeight - 1; i >= 0; i--)
  268.     for (j = 0; j < ScreenWidth; j++)
  269.         *PBuffer++ = ColorMap[ScreenBuffer[i][j]].Red +
  270.                  (ColorMap[ScreenBuffer[i][j]].Green << 8) +
  271.                  (ColorMap[ScreenBuffer[i][j]].Blue << 16);
  272.  
  273.     reshapeviewport();
  274.     lrectwrite(0, 0, ScreenWidth - 1, ScreenHeight - 1, IrisScreenBuffer);
  275.  
  276.     while (TRUE) {
  277.     if (qread(&Val) == REDRAW) {
  278.         reshapeviewport();
  279.         lrectwrite(0, 0, ScreenWidth - 1, ScreenHeight - 1,
  280.                                                IrisScreenBuffer);
  281.     }
  282.     }
  283. }
  284.